<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<title>文章管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">标题</label>
						<div class="layui-input-inline">
							<input type="text" name="title" placeholder="" class="layui-input">
						</div>
						<label class="layui-form-label">分类</label>
						<div class="layui-input-inline">
							<input type="text" name="categoryName" placeholder="" class="layui-input">
						</div>
						<button class="pear-btn pear-btn-md pear-btn-primary query" lay-submit lay-filter="user-query">
							<i class="layui-icon layui-icon-search"></i>
							查询
						</button>
						<button type="reset" class="pear-btn pear-btn-md">
							<i class="layui-icon layui-icon-refresh"></i>
							重置
						</button>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="user-table" lay-filter="user-table"></table>
			</div>
		</div>

	<script type="text/html" id="user-toolbar">
		<button class="pear-btn pear-btn-primary pear-btn-md permission" permission = 'blogarticle:add'  lay-event="add" >
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
		<button class="pear-btn pear-btn-danger pear-btn-md permission" permission = 'blogarticle:del' lay-event="batchRemove" >
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>

	<script type="text/html" id="user-bar">
		<button class="pear-btn pear-btn-primary pear-btn-sm permission" permission = 'blogarticle:update' lay-event="edit"><i class="layui-icon layui-icon-edit" ></i></button>
<!--        <button class="pear-btn pear-btn-warming pear-btn-sm permission" permission = 'blogarticle:power' lay-event="power"><i class="layui-icon layui-icon-vercode"></i></button>-->
		<button class="pear-btn pear-btn-danger pear-btn-sm permission" permission = 'blogarticle:del' lay-event="remove"><i class="layui-icon layui-icon-delete" ></i></button>
<!--        <button class="pear-btn pear-btn-sm" id="more_{{d.userId}}"><i class="layui-icon layui-icon-triangle-d"></i></button>-->
	</script>

	<script type="text/html" id="user-enable">
		<input type="checkbox" title="{{d.title}}" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{ d.status== 1 ? 'checked' : '' }} />
	</script>

        <script type="text/html" id="image">
            <img src="{{d.articleCover}}" style="max-height: 45px" onclick="imgcli(this)">
        </script>

        <!--图片放大查看相关样式及div-->
        <style type="text/css">
            .layui-table-cell{
                text-align:center;
                height: auto;
                white-space: normal;
            }
            .layui-table img{
                max-width:100px
            }
        </style>
        <div  id="divimg" style="cursor:zoom-out;background: rgba(0,0,0,.6);width:100%;height:100%;position: absolute;z-index: 9999;left: 0;top: 0;display: none">
            <img style="width: 300px;left: 50%;top:50%;margin-left: -200px;position: absolute;">
        </div>



        <script type="text/html" id="user-sex">
		{{#if (d.original == 1) { }}
		<span>是</span>
		{{# }else if(d.original == 0){ }}
		<span>否</span>
		{{# } }}
	</script>

	<script type="text/html" id="user-login">
		{{#if (d.login == 0) { }}
		<span>在线</span>
		{{# }else if(d.sex == 1){ }}
		<span>离线</span>
		{{# } }}
	</script>

	<script type="text/html" id="user-createTime">
		{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
	</script>



		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
        <script src="../../js/baseUrl.js"></script>
        <script src="../../js/cookieUtils.js"></script>
        <script src="../../component/pear/module/ajaxRequest.js"></script>
        <script src="../../js/jquery-3.2.1.min.js"></script>
        <script src="../../js/permission.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'drawer','ajaxRequest','toast', 'layer','code','element'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let drawer = layui.drawer;
                let ajaxRequest = layui.ajaxRequest;
                let toast = layui.toast;

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '标题',
							field: 'title',
							align: 'center',
							width: 200
						},
						{
							title: '分类名称',
							field: 'categoryName',
							align: 'center',
                            width: 100,
						},
                        {
                            title: '状态',
                            field: 'status',
                            align: 'center',
                            templet: '#user-enable',
                            width: 100,
                        },
                        {
                            title: '文章封面图',
                            field: 'articleCover',
                            align: 'center',
                            templet: '#image'
                        },
                        {
                            title: '作者',
                            field: 'author',
                            align: 'center'
                        },
						{
							title: '是否原创',
							field: 'original',
							align: 'center',
                            templet: '#user-sex'
						},
						{
							title: '创建时间',
							field: 'createdTime',
							align: 'center',
                            templet: '#user-createTime',
						},
						/*{
							title: '更新人',
							field: 'updatedBy',
							align: 'center'
						},
						{
							title: '更新时间',
							field: 'updatedTime',
							align: 'center',
							templet: '#user-createTime'
						},*/
						{
							title: '操作',
							toolbar: '#user-bar',
							align: 'left',
							width: 160,
							fixed: 'right'
						}
					]
				]

				var tableName = table.render({
					elem: '#user-table',
					url: baseUrl + "/admin/article/getArticleList",
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#user-toolbar',
                    headers: {'Authorization': tokenPrefix + token},
					defaultToolbar: [{
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports'],
					done: function(res, curr, count) {
					    // 添加权限验证
					    validaPermission();
					    // 添加角色验证
					    // validaRole();
                        }
				});

				table.on('tool(user-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					} else if(obj.event === 'power'){
                        window.power(obj);
                    }
				});

				table.on('toolbar(user-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(user-query)', function(data) {
				    // console.log(data.field)
					table.reload('user-table', {
						where: data.field
					})
					return false;
				});

				form.on('switch(user-enable)', function(obj) {
                    var data = new Object();
                    data.id = this.value;
                    if (obj.elem.checked) {
                        data.status = 1;
                    }else {
                        data.status = 0;
                    }
                    var res = ajaxRequest.sendPost("/admin/article/updateStatus",data,false,true);
                    if (res) {
                        toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                        layer.tips(this.title + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
                    }
				});

                let body;
				window.add = function(obj) {
                    let index1 = layer.open({
                        type: 2,
                        title: '添加文章',
                        shade: 0.1,
                        area: ['100%', '100%'],
                        // area: '80%',
                        content: './addArticle.html',
                        success: function(layero, index){
                            body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            // console.log(body.html()) //得到iframe页的body内容
                            // body.find('#aid').val(obj.data.id)
                        }
                        ,maxmin : true
                        ,btn: ['保存','关闭']
                        ,btnAlign: 'c' //按钮居中
                        ,shade: 0 //不显示遮罩
                        ,yes: function(){
                            body.find('#user-save').click();
                            layer.close(index1);
                            $('.query').click();
                        }
                    });
				};

				window.edit = function(obj) {
                    let index1 = layer.open({
                        type: 2,
                        title: '添加文章',
                        shade: 0.1,
                        area: ['100%', '100%'],
                        // area: '80%',
                        content: './addArticle.html',
                        success: function(layero, index){
                            body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            body.find('#articleId').val(obj.data.id)
                        }
                        ,maxmin : true
                        ,btn: ['保存','关闭']
                        ,btnAlign: 'c' //按钮居中
                        ,shade: 0 //不显示遮罩
                        ,yes: function(){
                            body.find('#user-save').click();
                            layer.close(index1);
                            $('.query').click();
                        }
                    });

				}


				window.remove = function(obj) {
					layer.confirm('确定要删除该文章吗', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();

                        var res = ajaxRequest.sendPost("/admin/article/delBlogArticle",{'id':obj.data['id']},false,true);
                        if (res){
                            layer.close(loading);
                            toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                            $('.query').click();
                        }
					});
				}

				window.batchRemove = function(obj) {
					let data = table.checkStatus(obj.config.id).data;
					if (data.length === 0) {
						layer.msg("未选中任何数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					let ids = "";
					for (let i = 0; i < data.length; i++) {
						ids += data[i].id + ",";
					}
					ids = ids.substr(0, ids.length - 1);
					layer.confirm('确定要删除这些文章吗', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
                        var res = ajaxRequest.sendPost("/admin/article/delBlogArticleIds",{'ids':ids},false,true);
                        if (res){
                            layer.close(loading);
                            toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                            $('.query').click();
                        }

					});
				}
				window.refresh = function(param) {
					table.reload('user-table');
				}

			})

            // 放大图片
            $('#divimg').click(function(){$(this).fadeOut(200)});
            function imgcli(obj){
                $('#divimg').fadeIn(200);
                $('#divimg img').attr('src',$(obj).attr("src"));
                $('#divimg img').css("margin-top","-"+$('#divimg img').height()/2+"px")
            }
            
		</script>
	</body>
</html>
